<template>
    <!-- 调价结算列表 -->
    <div class="e-table billList">
        <TopStep :stepInfo="stepInfo" />
        <div class="top">
            <div class="left" style="margin-left: 10px">
                <div>
                    <el-button
                        size="mini"
                        @click="buttonAdd"
                        style="background: #7aa818; color: #fff"
                        v-permission="['edit']"
                        >新增</el-button
                    >
                </div>
            </div>
            <div class="right">
                <div class="right-div1">
                    <el-input
                        placeholder=""
                        v-model="query.keyword"
                        size="small"
                        @blur='getData'
                        @keyup.enter.native="getData"
                    >
                        <i
                            slot="suffix"
                            class="el-icon-search like"
                            @click="getData"
                        ></i>
                    </el-input>
                </div>
                <div>
                    <el-button
                        size="small"
                        style="background: #2e61d7; color: #fff"
                        @click="onAdvancedSearch"
                        >高级搜索</el-button
                    >
                </div>
            </div>
        </div>

        <el-table
            border
            ref="table"
            :data="tableData"
            style="width: 100%"
            class="table"
            highlight-current-row
            height="0"
            v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
        >
            <el-table-column
                label="结算编号"
                min-width="300"
                fixed
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span
                        @click="Examine(scope.row)"
                        style="color: blue; cursor: pointer"
                    >
                        {{ scope.row.billNo }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="settlementDate" label="结算日期" width="150" fixed>
            </el-table-column>
            <el-table-column
                prop="settlementUnitName"
                label="结算单位"
                width="100"
                show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
                prop="businessType"
                label="结算类型"
                width="100"
                show-overflow-tooltip
            >
            <template v-slot='{row}'>
                {{['零星采购计划','物资采购合同','调拨单'][row.businessType]}}
            </template>
            </el-table-column>
            <el-table-column
                prop="amount"
                label="结算金额（元）"
                width="150"
                align="right"
            >
            </el-table-column>
            <el-table-column
                prop=""
                label="累计结算金额（元）"
                width="170"
                align="right"
            >
            </el-table-column>
            <el-table-column label="状态" show-overflow-tooltip width="100">
                <template slot-scope="scope">
                    <ToolHistory
                        v-if="scope.row.state === 1"
                        :key="scope.$index"
                        url="/turnover/audit/getHistoryList"
                        :billId="scope.row.billId"
                        :billType="1036"
                    >
                        <span v-status="scope.row.state">
                            {{ showState(scope.row.state) }}
                        </span>
                    </ToolHistory>
                    <span v-status="scope.row.state" v-else>{{showState(scope.row.state)}}</span>
                </template>
            </el-table-column>
            <el-table-column label="红单状态" width="100">
                <template v-slot="{ row }">
                    <span
                        style="color: blue; cursor: pointer"
                        @click="redClick(row)"
                        >{{ row.redState }}</span
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="orgName"
                label="所属机构"
                min-width="200"
                show-overflow-tooltip
                align="left"
            >
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <ComPagination
            :total="total"
            :pageSize.sync="query.limit"
            :currentPage.sync="query.page"
            @currentChange="getData"
            @sizeChange="getData"
            class="bottomSetting"
        />
        <!-- 高级搜索 -->
    </div>
</template>
<script>
import ComPagination from '@/components/pagination/pagination.vue'
import request from '@/api/materials/materialAcceptance'
import priceSetRequest from '@/api/materialSettlement/priceSettlement'
import { vueDom, debounce, advanceSearch, showLoading, hideLoading } from '@/utils/common.js'
import TopStep from '@/components/topstep/topstep'
import { mapMutations } from 'vuex'

export default {
    components: {
        ComPagination,
        TopStep
    },
    data () {
        return {
            tableData: [],
            query: {
                beginPlanDate: '',
                businessType: [],
                endPlanDate: '',
                keyword: '',
                limit: 20,
                orgId: '',
                page: 1,
                settlementUnitId: '',
                states: []
            },
            total: 1,
            tableHeight: 0,
            auditParams: {
                billId: '',
                billType: 1036,
                freeNextUnitIndex: null,
                freeNextUser: '',
                isPass: true,
                isShareInAudit: true,
                opinion: '',
                orgId: '',
                orgName: '',
                userId: '',
                workId: ''
            },
            stepInfo: [
                {
                    description: '现场收料'
                },
                {
                    description: '物资验收'
                },
                {
                    description: '采购结算'
                },
                {
                    description: '调价验收'
                },
                {
                    description: '调价结算'
                }
            ]
        }
    },
    computed: {
        // //获取状态
        // statusLable () {
        //     return param => {
        //         const state = this.$store.state.settlementManagement.ysState
        //         const obj = state.find(x => x.value === Number(param))
        //         return obj.label
        //     }
        // }
    },
    created () {
        this.getData()
    },
    mounted () {
        // table动态高度
        vueDom(
            () => {
                this.tableHeight = document.body.clientHeight - 240
            },
            () => {
                return $('table').length !== 0
            }
        )
        this.fn = () => {
            this.tableHeight = document.body.clientHeight - 240
        }
        window.onresize = debounce(this.fn)
    },
    methods: {
        ...mapMutations(['setSelectedInfo']),
        // 获取列表数据
        async getData () {
            try{
                showLoading()
                let res = await priceSetRequest.getData(this.query)
                let data = res.list || []
                data.forEach(item => {
                    switch (item.redBillState) {
                    // 用redState来表示值的目的是:不改变原有的redBillState的数字值,否则在跳转页面时不方便做判断
                    case -1:
                        item.redState = ''
                        break
                    case 0:
                        item.redState = '冲销中'
                        break
                    case 1:
                        item.redState = '冲销中'
                        break
                    case 2:
                        item.redState = '已冲销'
                        break
                    case 3:
                        item.redState = '红单'
                        break
                    }
                })
                this.tableData = data
                this.total = res.totalCount
            }finally{
                hideLoading()
            }
        },
        //新增按钮
        buttonAdd () {
            this.$router.push('/priceSettlementAdd')
        },

        //详情
        Examine (row) {
            if (row.state === -1 || row.state === 2 || row.state === 5 || !this.permission(['edit'])) {
                this.$router.push({
                    path: '/priceSettlementView',
                    query: { billid: row.billId }
                })
            } else {
                this.auditParams.billId = row.billId
                request.getCurrentUnitInfo(this.auditParams).then(res => {
                    if (res.canModify == true) {
                        this.$router.push({
                            path: '/priceSettlementEdit',
                            query: { billid: row.billId }
                        })
                    } else {
                        this.$router.push({
                            path: '/priceSettlementView',
                            query: { billid: row.billId }
                        })
                    }
                })
            }
        },

        // redBillState的取值: (-1:当前单据是普通单据; 0:当前单据是红单的草稿状态; 1:当前单据是红单的审核中状态; 2:当前单据是红单的已审核; 3:当前是红单,在点击时需要跳转到源单)
        redClick (row) {
            this.auditParams.billId = row.billId
            if (row.redBillState == 3) {
                // 如果该单据就是红单,则点击时进入源单详情页
                this.$router.push({
                    path: '/priceSettlementView',
                    query: { billid: row.sourceId }
                })
            } else {
                // 如果点击的该单据不是红单,则点击超链接进入红单
                // 如果进入红单,还需判断进入该红单的编辑页还是详情页
                if (row.redBillState == 0) {
                    this.$router.push({
                        path: '/priceSettlementEdit',
                        query: { billid: row.redBillId }
                    })
                } else if (row.redBillState == 1 || row.redBillState == 2) {
                    this.$router.push({
                        path: '/priceSettlementView',
                        query: { billid: row.redBillId }
                    })
                }
            }
        },
        // 高级搜索
        onAdvancedSearch () {
            localStorage.setItem('acceptance', 'price')
            advanceSearch('/materialsAccAdvancedSearch').then(res => {
                this.query.beginPlanDate = res.beginPlanDate
                this.query.businessType = res.businessType
                this.query.endPlanDate = res.endPlanDate
                this.query.orgId = res.orgId
                this.query.states = res.states
                this.query.supplierId = res.supplierId
                // this.setSelectedInfo(this.query)
                // openAppClient('/showSelectedData', '搜索参数')
                this.getData()
            })
        },
        showState (state) {
            const _state = this.$store.state.settlementManagement.ysState
            const obj = _state.find(x => x.value === Number(state))
            return obj.label
        }
    }
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed{
    height: 98.5% !important;
    bottom: 17px !important;
}
::v-deep .el-table__fixed-body-wrapper {
    height: 100% !important;
}
</style>
